package cn.youqu.happy.module.youqu.controller.app.userFollows;

import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.userFollows.vo.UserFollowsPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userFollows.vo.AppUserFollowsFollowReqVO;
import cn.youqu.happy.module.youqu.controller.app.userFollows.vo.AppUserFollowsPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userFollows.vo.AppUserFollowsRespVO;
import cn.youqu.happy.module.youqu.controller.app.userFollows.vo.AppUserFollowsUpdateReqVO;
import cn.youqu.happy.module.youqu.convert.userFollows.UserFollowsConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userFollows.UserFollowsDO;
import cn.youqu.happy.module.youqu.service.userFollows.UserFollowsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 用户关注活动")
@RestController
@RequestMapping("/youqu/user-follows")
@Validated
public class AppUserFollowsController {

    @Resource
    private UserFollowsService userFollowsService;

    @PostMapping("/follow")
    @ApiOperation("创建用户关注活动")
    @PreAuthenticated
    public CommonResult<Integer> followUserFollows(@Valid @RequestBody AppUserFollowsFollowReqVO followReqVO) {
        Long loginUserId = getLoginUserId();
        return success(userFollowsService.followerFollows(followReqVO, loginUserId));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户关注活动")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserFollows(@Valid @RequestBody AppUserFollowsUpdateReqVO updateReqVO) {
        Long loginUserId = getLoginUserId();

        userFollowsService.updateUserFollows(updateReqVO,loginUserId);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户关注活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserFollows(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();

        userFollowsService.deleteUserFollows(id, loginUserId);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户关注活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<AppUserFollowsRespVO> getUserFollows(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();

        UserFollowsDO userFollows = userFollowsService.getUserFollows(id, loginUserId);
        return success(UserFollowsConvert.INSTANCE.convert0(userFollows));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户关注活动列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated
    public CommonResult<List<AppUserFollowsRespVO>> getUserFollowsList(@RequestParam("ids") Collection<Integer> ids) {
        Long loginUserId = getLoginUserId();
        List<UserFollowsDO> list = userFollowsService.getUserFollowsList(ids, loginUserId);
        return success(UserFollowsConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户关注活动分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserFollowsRespVO>> getUserFollowsPage(@Valid AppUserFollowsPageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        UserFollowsPageReqVO convert = UserFollowsConvert.INSTANCE.convert(pageVO);
        PageResult<UserFollowsDO> pageResult = userFollowsService.getUserFollowsPage(convert, loginUserId);
        return success(UserFollowsConvert.INSTANCE.convertPage0(pageResult));
    }


}
